//
//  MQSearchHistoryCell.swift
//  MQCRM
//
//  Created by 许博 on 2017/5/8.
//  Copyright © 2017年 mechat. All rights reserved.
//

import UIKit

@objc protocol SearchHistoryDeleteSingleDeletge {
    @objc optional func deleteSingeData(data:String)
}

class MQSearchHistoryCell: UITableViewCell {

    
    var delegate:SearchHistoryDeleteSingleDeletge?
    var title:String? {
        didSet{
            nameLabel.text = title
 
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        selectionStyle = .none
        contentView.addSubview(nameLabel)
        contentView.addSubview(searchIconView)
        contentView.addSubview(deleteBtn)
    }
    
    
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        searchIconView.frame = CGRect(x: KCommonCellPadding, y: 10, width: 24, height: 24)
        nameLabel.frame = CGRect(x: searchIconView.frame.maxX + 10, y: 12 , width: KScreenW - 70, height: 20)
        deleteBtn.frame = CGRect(x: KScreenW - KCommonCellPadding - 14 , y: 15, width: 14, height: 14)
        
        
    }
    
    // 删除单条历史记录
    @objc fileprivate func deleteHistory(){
        print(#function)
        guard let title = title else {
            return
        }
        delegate?.deleteSingeData?(data:title)
    }
    
    fileprivate lazy var nameLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.charcoalGrey
        return label
    }()
    
    fileprivate lazy var searchIconView:UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "icMagnifierGrey")
        return imgView
    }()
    
    fileprivate lazy var deleteBtn:UIButton = {
        let btn = UIButton(type:.custom)
        btn.setImage(UIImage(named:"btnCloseGrey"), for: .normal)
        btn.addTarget(self, action: #selector(deleteHistory), for: .touchUpInside)
        return btn
    }()
}
